package com.pig4cloud.pigx.contract.executer.order.initializer.impl;

import com.pig4cloud.pigx.common.customize.enums.CommonDictEnum;
import com.pig4cloud.pigx.common.customize.mysql.utils.MysqlUtil;
import com.pig4cloud.pigx.common.customize.security.utils.SecurityUtil;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.contract.bo.ContractOrderNewBO;
import com.pig4cloud.pigx.contract.constant.ContractMqConstants;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.executer.order.initializer.OrderInitializerManager;
import com.pig4cloud.pigx.contract.mapper.ContractOrderMapper;
import com.pig4cloud.pigx.contract.mapper.ContractPositionMapper;
import com.pig4cloud.pigx.contract.mongo.entity.ContractOrderMongo;
import com.pig4cloud.pigx.contract.mongo.entity.ContractSymbolMongo;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 机器人订单初始化器
 *
 * @since 2022/8/17 14:25
 */
@Component
public class RobotOrderInitializer extends AbstractOrderInitializer
        implements InitializingBean {

    protected final OrderInitializerManager orderInitializerManager;

    /**
     * 有参构造函数
     * <p>同时给父级对象赋值
     *
     * @since 2022/8/17 20:51
     */
    public RobotOrderInitializer(ContractOrderMapper contractOrderMapper,
                                 ContractPositionMapper contractPositionMapper,
                                 MongoTemplate mongo,
                                 AmqpTemplate rabbit,
                                 CommonUtil commonUtil,
                                 MysqlUtil mysqlUtil,
                                 SecurityUtil securityUtil,
                                 ContractUtils contractUtils,
                                 OrderInitializerManager orderInitializerManager) {
        super(contractOrderMapper, contractPositionMapper, mongo, rabbit, commonUtil, mysqlUtil, securityUtil, contractUtils);
        this.orderInitializerManager = orderInitializerManager;
    }

    /**
     * 当前对象创建后
     *
     * @since 2022/8/17 14:36
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        // 注册到manager
        orderInitializerManager.getInitializerList().add(this);
    }

    /**
     * 判断是否支持
     *
     * @param bo 入参
     * @return {@link boolean}
     * @since 2022/8/17 11:20
     */
    @Override
    public boolean support(ContractOrderNewBO bo) {
        return Objects.equals(CommonDictEnum.YesOrNo.YES.getValue(), bo.getIsRobot())
                && Objects.equals(ContractOrderEnum.Type.LIMIT.getValue(), bo.getType());
    }

    /**
     * 获得需要冻结的金额
     *
     * @param bo bo
     * @return {@link BigDecimal}
     * @since 2022/8/22
     */
    @Override
    protected BigDecimal getFrozenAmountWhenOpen(ContractOrderNewBO bo) {
        return BigDecimal.ZERO;
    }

    /**
     * 计算需要冻结的合约账户金额，开始冻结
     *
     * @param bo bo
     * @since 2022/8/17 18:46
     */
    @Override
    protected void frozenAssetWhenOpen(ContractOrderNewBO bo) {
    }

    /**
     * 初始化数据
     *
     * @param bo bo
     * @since 2022/8/16 22:22
     */
    @Override
    protected void initParam(ContractOrderNewBO bo) {
        ContractSymbolMongo symbol = contractUtils.getSymbolById(bo.getSymbolId());
        bo.setSymbol(symbol);
        super.initParam(bo);
    }

    /**
     * 校验仓位信息
     *
     * @param bo bo
     * @since 2022/8/16 21:25
     */
    @Override
    protected void validPosition(ContractOrderNewBO bo) {
    }

    /**
     * 校验币对信息
     *
     * @param bo bo
     * @since 2022/8/16 21:05
     */
    @Override
    protected void validSymbol(ContractOrderNewBO bo) {
    }

    /**
     * 校验bo
     *
     * @param bo bo
     * @since 2022/8/16 18:16
     */
    @Override
    protected void validParam(ContractOrderNewBO bo) {
        super.validParam(bo);
    }

    /**
     * 发送mq消息
     *
     * @param bo bo
     * @since 2022/8/17 20:49
     */
    @Override
    protected void sendMq(ContractOrderNewBO bo) {
        // 发送到机器人订单专属队列
        ContractOrderMongo msg = new ContractOrderMongo();
        msg.setId(bo.getId());
        msg.setIsBuy(bo.getIsBuy());
        rabbit.convertAndSend(ContractMqConstants.EXCHANGE_FANOUT_CONTRACT_ORDER_NEW_ROBOT_K_LINE,
                null,
                msg);

        // 发送深度图参数消息。深度图功能暂时弃用该消息队列，2022.9.7（可通过该条注释检索到其它深度图消息发送代码位置）
		/*RobotDepthMapMqBO depthMapMqBO = new RobotDepthMapMqBO();
		depthMapMqBO.setSymbolName(bo.getSymbolName());
		depthMapMqBO.setSide(bo.getSide());
		depthMapMqBO.setNum(bo.getNum());
		depthMapMqBO.setPrice(bo.getPrice());
		rabbit.convertAndSend(ContractMqConstants.EXCHANGE_FANOUT_CONTRACT_ORDER_NEW_ROBOT_K_LINE_DEPTH_MAP,
				null,
				depthMapMqBO);*/
    }
}
